#!/bin/bash

# 进入到目录，目录为第一个参数，第二个参数是合并分支，第三个参数是feature分支
if [ -z "$3" ]; then
	echo "Usage: $0 <Git Folder> <Merge Branch Name> <Feature Branch Name>"
	exit 1
fi

cd "$1" || exit
git checkout "$2"
git pull origin "$2"
git fetch origin main
git merge origin/main -m "[Auto] merge from [main] to [$2]"
if [ $? -ne 0 ]; then
  git merge --abort
  echo "合并主干失败，请手动解决冲突. merge --abort"
  exit 2
fi

git fetch origin "$3"
git merge origin/"$3" -m "[Auto] merge from [$3] to [$2]"
if [ $? -ne 0 ]; then
  git merge --abort
  echo "分支合并失败，请手动解决冲突. merge --abort"
  exit 1
fi
git push origin "$2"
echo "Merge Success"